package com.swufe.scoresforapp;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.Image;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.view.NestedScrollingParent2;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.swufe.scoresforapp.Yonghu.Dataimport;
import com.swufe.scoresforapp.Yonghu.Dataoutput;
import com.swufe.scoresforapp.bean.SchoolBean;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.carbs.android.avatarimageview.library.AvatarImageView;

public class ScrollingSchoolActivity extends AppCompatActivity{

    private TextView school_tel;  //招生电话
    private TextView school_place;  //院校所在地
    private TextView school_type;  //院校类别
    private TextView school_main_num;   //重点学科数
    private TextView school_part;  //所属部门
    private TextView school_shuoshi;  //硕士点数
    private TextView school_boshi;  //博士点数
    private TextView school_is_985;  //是否985
    private TextView school_is_211;  //是否211
    private SchoolBean School;
    private ImageView photo;//学校校徽
    private Handler handler;
    private String yonghu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scrolling_school);
        initData();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle(School.Schoolname);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        SharedPreferences preferences = getSharedPreferences("Data", Context.MODE_PRIVATE);
        yonghu = preferences.getString("Name","");
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if(msg.what == 13 ){
                    String result = (String)msg.obj;
                    Toast.makeText(ScrollingSchoolActivity.this ,result, 0).show();
                }
                super.handleMessage(msg);
            }
        };
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               new Thread(new Runnable() {
                   @Override
                   public void run() {
                       Dataoutput out = new Dataoutput();
                       Dataimport in = new Dataimport();
                       Message msg = handler.obtainMessage(13);
                       if(in.getShoucang(yonghu).contains(School.Schoolname))
                           msg.obj ="已收藏该学校";
                       else {
                           msg.obj = out.setShoucang(School.Schoolname, yonghu);
                       }
                       handler.sendMessage(msg);
                   }
               }).start();
            }
        });


    }

    private void initData(){
        school_tel = findViewById(R.id.school_tel);
        school_place = findViewById(R.id.school_place);
        school_type = findViewById(R.id.school_type);
        school_main_num = findViewById(R.id.school_main_num);
        school_part = findViewById(R.id.school_part);
        school_shuoshi = findViewById(R.id.school_shuoshi);
        school_boshi = findViewById(R.id.school_boshi);
        school_is_985 = findViewById(R.id.school_is_985);
        school_is_211 = findViewById(R.id.school_is_211);
        photo = (ImageView) findViewById(R.id.image);
        Intent intent = getIntent();
        String JsonData = intent.getStringExtra("information");
        School = new Gson().fromJson(JsonData,SchoolBean.class);
        school_tel.setText(School.phone);
        school_place.setText(School.location.split(":")[1]);
        school_type.setText(School.type.split(":")[1]);
        school_main_num.setText(School.majornum.split(":")[1]);
        school_part.setText(School.belong.split(":")[1]);
        school_shuoshi.setText(School.shuoshi.split(":")[1]);
        school_boshi.setText(School.boshi.split(":")[1]);
        school_is_985.setText(School.if985);
        school_is_211.setText(School.if211);
        GlideImageLoader loader = new GlideImageLoader();
        loader.displayImage(getBaseContext(),School.photosource,photo);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
